import pymysql
from pymysql import cursors


def get_connection():
    # 连接数据库
    conn = pymysql.connect(host='localhost',
                           user='root',
                           password='admin',
                           db='cov',
                           cursorclass=cursors.DictCursor,
                           charset='utf8')
    cursor = conn.cursor()
    return conn, cursor


def close_connection(conn, cursor):
    # 关闭数据库
    cursor.close()
    conn.close()

def areaCount(province):
    """
    :return:  根据省份查询当地数据
    """
    conn, cursor = get_connection()
    sql = "SELECT city,confirm FROM details WHERE province like %s"
    cursor.execute(sql, '%'+province+'%')
    result = cursor.fetchall()
    close_connection(conn, cursor)
    return result

def areaPie(province):
    """
    :return:  根据省份查询当地数据
    """
    conn, cursor = get_connection()
    sql = "SELECT confirm 'value',city 'name' FROM details WHERE province like %s"
    cursor.execute(sql, '%'+province+'%')
    result = cursor.fetchall()
    close_connection(conn, cursor)
    return result